#include "head.h"


char login_name[N];			//取用户名
char login_passwd[N];		//取用户密码
	
	
int do_login(int sockfd, MSG *pbuf)
{
	MSG rmsg;		//接收结构体
	
	puts("************************************************");
	puts("****************Welcome to Login****************");
	puts("************************************************");
	
	while(1){
		bzero(pbuf, sizeof(pbuf));					//清空发送结构体
		/*设置登录结构体包*/
		pbuf->type = LOGIN;							//设置操作类型登录
		printf("Please input user name : ");
		scanf("%s", pbuf->name);
		printf("Please input password :  ");
		scanf("%s", pbuf->passwd);
		send(sockfd, pbuf, sizeof(MSG), 0);//发送
		
		if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
			perror("recv");
			puts("Please input again!");
			continue;
		}
		/*判断登录是否成功*/
		printf("ret:%d\n",rmsg.ret);						
		if(rmsg.ret==ERROR){								//不成功，一直循环
			puts("User Info is error!");				
			puts("Please input again!");
		}
		else{												//成功返回操作结果
			puts("User login success!");
			strcpy(login_name, pbuf->name);//取用户名
			strcpy(login_passwd, pbuf->passwd);//取用户密码
			return rmsg.ret;
		}	
	}
    
}

int do_stuff(int sockfd, MSG *pbuf)
{
    puts("***************************************************");
	puts("****************It is do_stuff page****************");
	puts("***************************************************");
	puts("");
	
	int select = 0;		//选择操作位
	MSG rmsg;		//接收结构体
	
	char pass1[N],pass2[N];
	
	while(1){
		puts("******************************************************************");
		puts("*************1、改 ** 2、查 ** 3、退出**4、返回上一层*************");
		puts("******************************************************************");
		puts("");
		
		bzero(pbuf, sizeof(pbuf));			//清空发送结构体	
		
		printf("Please input select : ");
		scanf("%d",&select);
		getchar();//消除输入错误的BUG
		printf("It is %d\n",select);
		
		switch(select){
			case 1:											//改
				pbuf->type =STAFF_CHANGE_PASSWD;//设置修改密码标志
				strcpy(pbuf->info.name, login_name);
				printf("name:         %s\n",pbuf->info.name);
				printf("old password: %s\n",login_passwd);
				printf("Please input chang pass:");
				scanf("%s",pass1);
				printf("Please input chang pass again:");
				scanf("%s",pass2);
				if(!strcmp(pass1,pass2)){
					strcpy(pbuf->data, pass1);
				}
				else{
					puts("Two passwords are different!");
					puts("Please input again!");
					break;
				}
				send(sockfd, pbuf, sizeof(MSG), 0);//发送
				
				if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
					perror("recv");
					puts("Please input again!");
					break;
				}
				if(rmsg.ret == 1){
					printf("Change is success!\n");//修改成功
					puts("Please login again!");
					return 0;
				}
				else{
					printf("Change is fail!\n");//修改失败
					puts("Please input again!");
				}
				break;
			case 2:											//查
				pbuf->type=STAFF_QUERY;
				strcpy(pbuf->info.name, login_name);
				send(sockfd, pbuf, sizeof(MSG), 0);
				
				if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
					perror("recv");
					puts("Please input again!");
					break;
				}
				if(rmsg.ret == 2){
					printf("%s\n", rmsg.data);//打印查询用户信息
				}
				else{
					puts("Query is error!");
					puts("Please input again!");
				}
				break;
			case 3:											//退出
				send(sockfd, pbuf, 0, 0);					
				close(sockfd);
				exit(1);
			case 4:
				return 0;
			default:
				puts("It is error");
				break;
		}
	}
}

int do_admin(int sockfd, MSG *pbuf)
{
	puts("***************************************************");
	puts("****************It is do_admin page****************");
	puts("***************************************************");
	puts("");
	
	int select = 0;		//选择操作位
	MSG rmsg;		//接收结构体
	char staff_name[N];//取员工名 
	
	// char admin_name[N] = *(pbuf->name);			//取用户名
    // char admin_passwd[N] = *(pbuf->passwd);		//取用户密码
	
	while(1){
		puts("******************************************************************");
		puts("****1、增 ** 2、删 ** 3、改 ** 4、查 ** 5、退出**6、返回上一层****");
		puts("******************************************************************");
		puts("");
		
		bzero(pbuf, sizeof(pbuf));			//清空发送结构体	
		
		printf("Please input select : ");
		scanf("%d",&select);
		getchar();//消除输入错误的BUG
		printf("It is %d\n",select);
		
		switch(select){
			case 1:											//增
				pbuf->type =ADMIN_ADD_STAFF;//设置增加用户标志
				printf("input id:");
				scanf("%d", &(pbuf->info.id));
				printf("input name:");
				scanf("%s", pbuf->info.name);
				printf("input pass:");
				scanf("%s", pbuf->passwd);
				printf("input age:");
				scanf("%d", &(pbuf->info.age));
				printf("input addr:");
				scanf("%s", pbuf->info.addr);
				printf("input salary:");
				scanf("%f", &(pbuf->info.salary));  				
				send(sockfd, pbuf, sizeof(MSG), 0);//发送
				
				if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
					perror("recv");
					break;
				}
				
				if(rmsg.ret == 1){
					printf("Insert is success!\n");//添加成功
				}
				else{
					printf("Insert is fail!\n");//添加失败
				}
				break;
			case 2:											//删
				pbuf->type = ADMIN_DEL_STAFF;//设置操作类型删除
				printf("Please input del staff name :");
				scanf("%s", pbuf->info.name);
				send(sockfd, pbuf, sizeof(MSG), 0);//发送
				if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
					perror("recv");
					puts("Please input again!");
					break;
				}
				printf("ret:%d\n",rmsg.ret);						//判断删除是否成功
				if(rmsg.ret==ERROR){	//不成功，一直循环
					puts("delete Info is error!");				
					//puts("Please input again!");
				}
				else{					//成功返回操作结果
					puts("delete success!");
				}	
				break;
			case 3:											//改
				pbuf->type =ADMIN_CHANGE_STAFF;//设置修改用户标志
				
				printf("Please input chang name:");
				scanf("%s",pbuf->data);
				strcpy(staff_name, pbuf->data);
				send(sockfd, pbuf, sizeof(MSG), 0);//发送
				
				if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
					perror("recv");
					puts("Please input again!");
					break;
				}
				if(rmsg.ret == 3){
					bzero(pbuf, sizeof(pbuf));//清空发送结构体
					
					printf("%s\n", rmsg.data);//打印员工信息
					
					printf("input id:");
					scanf("%d", &(pbuf->info.id));
					strcpy(pbuf->info.name, staff_name);
					// printf("input pass:");
					// scanf("%s", pbuf->data);
					printf("input age:");
					scanf("%d", &(pbuf->info.age));
					printf("input addr:");
					scanf("%s", pbuf->info.addr);
					printf("input salary:");
					scanf("%f", &(pbuf->info.salary));  				
					send(sockfd, pbuf, sizeof(MSG), 0);//发送
				}
				else{
					printf("Change is fail!\n");
					puts("Please input again!");
					break;
				}
				
				if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
					perror("recv");
					puts("Please input again!");
					break;
				}
				if(rmsg.ret == 3){
					printf("Change is success!\n");//修改成功
				}
				else{
					printf("Change is fail!\n");//修改失败
					puts("Please input again!");
				}
				break;
			case 4:											//查
				/*设置查询结构体包*/
				pbuf->type = ADMIN_QUERY;					
				printf("Please input query name : ");
				scanf("%s",  pbuf->info.name);
				send(sockfd, pbuf, sizeof(MSG), 0);	//发送	
				
				if(recv(sockfd, &rmsg, sizeof(MSG), 0) == -1){//等待接收
					perror("recv");
					puts("Please input again!");
					break;
				}
				if(rmsg.ret == 4){
					printf("%s\n", rmsg.data);//打印查询用户信息
				}
				else{
					puts("Query is error!");
					puts("Please input again!");
				}
				
				break;
			case 5:											//退出
				send(sockfd, pbuf, 0, 0);					
				close(sockfd);
				exit(1);
			case 6:
				return 0;
			default:
				puts("It is error");
				break;
		}
	}
	
}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf ;			//发送结构体
    char clean[64];

    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = PF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    //连接服务器
    if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("fail to connect");
        exit(-1);
    }
	while(1){
		ret = do_login(sockfd, &buf);
		if(ret == STAFF){  //员工
			puts("stuff login");
			do_stuff(sockfd, &buf);
		}
		else if(ret == ADMIN){  //管理员
			puts("admin login");
			do_admin(sockfd, &buf);
		}
	}
}
